# -*- coding: utf-8 -*-
from django.http import Http404, HttpResponse
from django.template import Template, Context
from django.contrib import auth
from django.shortcuts import  *
from django.contrib.auth.models import User
import datetime
import re
# is config file and main include 
from  gameCMS.main.admin.include.prof_class import *
from gameCMS.main.sampleapp.messages import *
from gameCMS.main.include.db import *
from gameCMS.main.include.send_email import *
def active(request):
    gh=0
    if  'email3' in request.GET and 'login' in request.GET and 'gen' in request.GET :
      r2= ur"^[0-9A-Za-z_.]{2,22}\@[A-Za-z.-]{2,22}\.[A-Za-z]{2,3}$"
      r1= ur'^[0-9A-Za-z_.]{6,22}$'
      r3= ur'^[0-9A-Za-z_.]{6,26}$'
      gen=re.search(r3,request.GET['gen'])
      nickname=re.search(r1,request.GET['login']) 
      email2=re.search(r2,request.GET['email3'])   
      if nickname   and  email2 and gen  : 
           nickname=request.GET['login'] 
           email2=request.GET['email3'] 
           gen=request.GET['gen']
           user = User.objects.get(username=nickname)
           if user.is_active==False and  user.last_name==gen and user.email==email2 and user.username==nickname: 
            user.last_name='none'
            user.is_active=True 
            user.save()
            gh=1
    return gh
def reg_main(request):
  tg=0
  reg=' '
  reg3=' '
  reg2=' '
  ty=' '
  if  'rt' in request.GET:
     tg=2  
     return tg,reg,reg2,reg3;
  if 'nickname_r' in request.POST and 'email_r' in request.POST:
   r2= ur"^[0-9A-Za-z_.]{2,22}\@[A-Za-z.-]{2,22}\.[A-Za-z]{2,3}$"
   r1= ur'^[0-9A-Za-z_.]{6,22}$'
   nickname=re.search(r1,request.POST['nickname_r']) 
   email2=re.search(r2,request.POST['email_r'])   
   if nickname   and  email2 : 
       nickname=request.POST['nickname_r'] 
       email2=request.POST['email_r'] 
       if SearchUser(nickname)==True: 
            if Searchemail(email2)==True:
                 user = User.objects.get(username=nickname)
                 if user.is_active==True:
                   gen= User.objects.make_random_password(length=7, allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789')
                   user.set_password(gen)
                   user.save()
                   email_rp(nickname,gen,email2)
                   tg=1
                 else:
                    user.save()
                    tg=2
                    reg2='вы не активировали свой профиль ' 
            else:
              tg=2
              reg2='email не существует' # add Check is activate
       else:
         tg=2
         reg2='такой ник не существует   '
   else:
      reg2='Некорректно введены данные'
      tg=2 
  if 'nickname' in request.POST and 'email' in request.POST and 'name' in request.POST and 'password2' in request.POST and 'password' in request.POST: 
     r2= ur"^[0-9A-Za-z_.]{2,22}\@[A-Za-z.-]{2,22}\.[A-Za-z]{2,3}$"
     r1= ur'^[0-9A-Za-z]{6,22}$'
     r3= ur'^[A-Za-z]{6,22}$'
     nickname=re.search(r1,request.POST['nickname']) 
     email2=re.search(r2,request.POST['email']) 
     name=re.search(r1,request.POST['name']) 
     password=re.search(r1,request.POST['password'])
     password2=re.search(r1,request.POST['password2'])
     if nickname   and  email2  and  name  and  password  and  password2 and request.POST['password2'] ==request.POST['password']: 
        nickname=request.POST['nickname'] 
        email2=request.POST['email'] 
        name=request.POST['name'] 
        password2=request.POST['password']
        if SearchUser(nickname)==False: 
          if Searchemail(email2)==False:
              user = User.objects.create_user(username=nickname,email=email2,password=password2)
              user.first_name=name
              id_d=user.id
              user.is_staff=False
              user.is_active=False
              gen= User.objects.make_random_password(length=25, allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789')
              user.last_name=gen
              Df=profile(id_d,'new')
              user.save()
              email_send(nickname,password2,email2,gen)
              tg=1     
          reg3='Такой email уже имеет пользователя   ' # add Check is activate
        reg2='Такой ник существует   '
     reg='Некорректно введены данные' # add logic bases message 
  return tg,reg,reg2,reg3,ty;
#-----session
def lgu(request):
    if 'lg' in request.GET:
     try:
         del request.session['US']
         del request.session['has']
         del request.session['lg']
         del request.session['ip']
          
     except KeyError:
         pass

def fin(request): 
   if 'has' in request.session and 'lg' in request.session and 'ip' in request.session and 'US' in request.session:
       if SearchUser(request.session['lg'])==True:
         gen= User.objects.make_random_password(length=25, allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789')
         user = User.objects.get(username=request.session['lg'])
         if user.last_name == request.session['has'] and request.session['ip']== request.META['REMOTE_ADDR'] and \
         request.session['US']==request.META['HTTP_USER_AGENT'] and user.username == request.session['lg']:
           request.session['has'] = gen
           user.last_name=gen
           user.save()
           return 1
         else:
           lgu(request)  
   return 0

def ent(request):
     if 'lg' in request.POST and 'pss' in request.POST:
      logins=request.POST['lg']
      passwd=request.POST['pss']
      user = auth.authenticate(username=logins, password=passwd)
      if user is not None and user.is_active:
        gen= User.objects.make_random_password(length=25, allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789')
        user3 = User.objects.get(username=logins)
        user3.last_name=gen
        user3.save()
        request.session['has'] = gen
        request.session['ip']=request.META['REMOTE_ADDR']
        request.session['US']=request.META['HTTP_USER_AGENT']
        request.session['lg'] = logins
        return gen
     return False
     



